package Controller;

import java.io.IOException;

import Dao.SignupDao;
import Utils.SimpleTools;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class SignupController extends Application {
    Stage stage=new Stage();
    @FXML//0
    private TextField usernameTextField;
    @FXML//1
    private TextField passwordTextField;
    @FXML//2
    private TextField nameTextField;
    @FXML//3
    private TextField idTextField;
    @FXML//4
    private TextField specialityTextField;
    @FXML//5
    private TextField typeTextField;
    @FXML//6
    private TextField phoneTextField;
    @FXML//7
    private TextField repasswordTextField;
    @FXML
    private Button logup;
    @FXML
    private Button signin;

    public void  showWindow() throws Exception {
        start(stage);
    }

    @FXML
    void logup(ActionEvent event) throws IOException {
        String[] properties = {usernameTextField.getText(),passwordTextField.getText(),nameTextField.getText()
                ,idTextField.getText(),specialityTextField.getText(),typeTextField.getText()
                ,phoneTextField.getText(),repasswordTextField.getText()};

        //判断是否全部输入
        if(SimpleTools.isempty(properties)){
            SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "抱歉", "请录入全部信息");
        }
        else{
            //判断用户名是否重复
            if(SignupDao.iscontain(properties[0])){
                SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "抱歉", "该用户名已注册");
            }
            else{
                //判断两次密码输入是否一致
                if(properties[1].equals(properties[7])){
                    //判断身份证号是否是18位
                    if(properties[3].length()!=18) SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "警告", "请正确输入身份证号");
                    else{
                        SignupDao.logup(properties);
                        Stage stage = (Stage)logup.getScene().getWindow();
                        stage.close();
                    }
                }
                else SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "错误", "两次密码输入不一致");
            }
        }
    }

    @FXML
    void Signin(ActionEvent event) {
        ((Stage)signin.getScene().getWindow()).close();
    }
    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("../View/SignupController.fxml"));
        primaryStage.setTitle("注册账号");
        primaryStage.setResizable(false);
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.getIcons().add(new Image("file:src/resource/flour.png"));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}